<?php
namespace Front\Controller;
use Common\Controller\AgentPublicController;

class MemController extends AgentPublicController {
    
    public function _initialize() {
        parent::_initialize();    
    }
    
    public function reg(){
        $agent_id=I('agent_id');
        $this->assign("agent_id",$agent_id);
        
        $agent_name=M('users')->where(array("id"=>$agent_id))->getField("user_login");
        $this->assign("agent_name",$agent_name);
        $this->display();
    }
    
    public function memberNameExists($name){
        return M('members')->where(array("username"=>$name))->find();
    }
    
    public function reg_post(){
        $name=I('name');
        $pwd=I('pwd');
        $confirm=I('confirm');
        $agent_id=I('agent_id');
         
        if(empty($name)||empty($pwd)||empty($confirm)||empty($agent_id)){
            $this->ajaxReturn(array("error"=>"1","msg"=>"参数不能为空"));
            exit;
         }
         
         $hs_validate_obj=new \HuoShu\Validate();
         $name_ok=$hs_validate_obj->username($name);
         if(!$name_ok){
             $this->ajaxReturn(array("error"=>"1","msg"=>"用户名长度需为6-20位，由字母和数字组成"));
            exit;
         }
         
         $pass_ok=$hs_validate_obj->password($pwd);
         if(!$pass_ok){
             $this->ajaxReturn(array("error"=>"1","msg"=>"密码长度需为6-16位"));
            exit;
         }
         
         if($pwd!==$confirm){
            $this->ajaxReturn(array("error"=>"1","msg"=>"两次密码输入不一致"));
            exit;
         }
         if($this->memberNameExists($name)){
            $this->ajaxReturn(array("error"=>"1","msg"=>"用户名已经被使用"));
            exit;
         }
         
         $hs_validate_obj=new \HuoShu\Validate();
         $is_valide_pass=$hs_validate_obj->password($pwd);
         if(!$is_valide_pass){
            $this->ajaxReturn(array("error"=>"1","msg"=>"密码格式不正确"));
            exit;
         }
         
         if(!$hs_validate_obj->username($name)){
            $this->ajaxReturn(array("error"=>"1","msg"=>"用户名格式不正确"));
            exit;
         }
         
         $mid=M('members')->add(array(
             "username"=>$name,
             "nickname"=>$name,
             "password"=>  $this->member_password($pwd),
             "agent_id"=>$agent_id,
             "status"=>"2",
             "reg_time"=>time()
         ));
         
//        $_SESSION['mid']=$mid;
//        $_SESSION['username']=$name;
        $this->ajaxReturn(array("error"=>"0","msg"=>"注册成功"));
    }
    
    function member_password($pw){
        $authcode=C("AUTHCODE");

        $result=md5(md5($authcode.$pw).$pw);

        return $result;
    }
}

